# --*-- coding:utf8 --*--
import copy
import json

from dataopts.down_merge import down_merge
from tools import conf
from mapping.dataNode import dataNode
from mapping.work import loadsheet, parentNode, PieData, sunburstData
from tools.path import cur_path

CURPATH = cur_path()


def parseData(name):
    sheet_object = loadsheet(name)
    nrows = sheet_object.nrows
    for row in range(1, nrows):
        insertNode(name, sheet_object, row)


def insertNode(name, itera, row):
    def insert(myNode, level):
        value = itera.cell_value(row, conf.automated_values[name]["colflag"][level])
        if myNode.version:
            return myNode.updateNode(
                [int(itera.cell_value(row, i)) for i in conf.automated_values[name]["colflag"][4:]], 1)
        if not myNode.son.get(value):
            Node = dataNode(value, len(conf.automated_values[name]["colflag"]) - 4,
                            start=itera.cell_value(row, conf.automated_values[name]["colflag"][4]),
                            end=itera.cell_value(row, conf.automated_values[name]["colflag"][5]))
            myNode.son[value] = Node
            if level == 2:
                Node.version = itera.cell_value(row, conf.automated_values[name]["colflag"][3])
            return myNode.updateNode(list(insert(Node, level + 1)), 4)
        else:
            if level == 2:
                if myNode.son.get(value).version <= itera.cell_value(row, conf.automated_values[name]["colflag"][3]):
                    myNode.son.get(value).version = itera.cell_value(row, conf.automated_values[name]["colflag"][3])
                    return myNode.updateNode(list(myNode.son.get(value).updateNode(
                        [int(itera.cell_value(row, i)) for i in conf.automated_values[name]["colflag"][4:]], 2)), 3)
            if myNode.son.get(value).changeV:
                myNode.son.get(value).changeV = False
                return myNode.updateNode(list(insert(myNode.son.get(value), level + 1)), 3)
            return myNode.updateNode(list(insert(myNode.son.get(value), level + 1)), 4)

    insert(parentNode, 0)


# def Dataforjson(index, name):
#     retData = dict()
#     innerData = dict()
#     retData['data'] = list()
#     for son_one in parentNode.son.keys():
#         innerData['name'] = son_one
#         innerData['value'] = parentNode.son[son_one].caseDetail[index]
#         innerData['children'] = list()
#         innerDatatwo = dict()
#         for son_two in parentNode.son[son_one].son.keys():
#             innerDatatwo['name'] = son_two
#             innerDatatwo['value'] = parentNode.son[son_one].son[son_two].caseDetail[index]
#             innerDatatwo['children'] = list()
#             innerDatathree = dict()
#             for son_three in parentNode.son[son_one].son[son_two].son.keys():
#                 innerDatathree['name'] = son_three
#                 innerDatathree['value'] = parentNode.son[son_one].son[son_two].son[son_three].caseDetail[index]
#                 innerDatatwo['children'].append(copy.deepcopy(innerDatathree))
#             innerData['children'].append(copy.deepcopy(innerDatatwo))
#         retData['data'].append(copy.deepcopy(innerData))
#
#     with open(CURPATH, '%s.json' % name, mode='w', encoding='utf-8') as fp:
#         json.dump(retData, fp, ensure_ascii=False)


# if __name__ == "__main__":
#     down_merge()
#     for name in conf.automated_values.keys():
#         parseData(name)
#
#         for x, y in enumerate(conf.automated_values[name]["colname"]):
#             # Pie.myPie(PieData(x), y)
#             Dataforjson(PieData(x), y)
